<template>
  <a-form class="form" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }" :model="formData" :rules="rules" @finish="onFinish">
    <a-form-item name="mavenHome" label="Maven目录">
      <path-input v-model:value="formData.mavenHome" />
    </a-form-item>
    <a-form-item name="repositoryUrl" label="仓库URL">
      <a-input v-model:value="formData.repositoryUrl" />
    </a-form-item>
    <a-form-item name="username" label="用户名">
      <a-input v-model:value="formData.username" />
    </a-form-item>
    <a-form-item name="password" label="密码">
      <a-input v-model:value="formData.password" />
    </a-form-item>
    <a-form-item name="respositroyDir" label="包目录">
      <path-input v-model:value="formData.respositroyDir" />
    </a-form-item>
    <a-form-item :wrapper-col="{ span: 8, offset: 8 }">
      <a-button type="primary" style="width: 100%" html-type="submit">上传</a-button>
    </a-form-item>
  </a-form>
</template>

<script setup>
import { reactive, onMounted } from 'vue'

const emit = defineEmits(['finish'])

const formData = reactive({
  mavenHome: 'd:/test/mvn-test/apache-maven-3.9.6',
  repositoryUrl: 'http://127.0.01:8081/repository/maven-releases/',
  username: 'admin',
  password: 'admin123',
  respositroyDir: 'd:/test/mvn-test/respository_bak',
})

onMounted(async () => {
  const mavenHome = await window.electronAPI.getMavenHome()
  formData.mavenHome = mavenHome
})


const rules = {
  mavenHome: [
    {
      required: true,
      message: '无效的路径',
      trigger: 'blur',
      validator: async (rule, val) => {
        const result = await window.electronAPI.isValidPath(`${val}/bin/mvn`)
        return result ? Promise.resolve() : Promise.reject()
      }
    }
  ],
  repositoryUrl: [
    {
      required: true,
      message: '请填写仓库URL',
      trigger: 'blur'
    }
  ],
  username: [
    {
      required: true,
      message: '请填写仓库用户名',
      trigger: 'blur'
    }
  ],
  password: [
    {
      required: true,
      message: '请填写仓库密码',
      trigger: 'blur'
    }
  ],
  respositroyDir: [
    {
      required: true,
      message: '无效的路径',
      trigger: 'blur',
      validator: async (rule, val) => {
        const result = await window.electronAPI.isValidPath(val)
        return result ? Promise.resolve() : Promise.reject()
      }
    }
  ]
}

const onFinish = values => {
  emit('finish', values)
}

</script>
<style scoped>
.form {
    width: 100%;
}
</style>
